package labox.innovation.gameserver.datatables;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javolution.util.FastMap;
import labox.innovation.config.Config;

/**
 * 获取传送价格配置信息
 * @author Frank
 *
 */
public class TeleportCostsConfig {
	private static final Logger _log = LoggerFactory.getLogger(TeleportCostsConfig.class.getName());

	private static final TeleportCostsConfig _instance = new TeleportCostsConfig();
	
	private FastMap<String,String> costsList = new FastMap<String, String>();
	
	private TeleportCostsConfig() {
		loadConfig();
	}
	
	public static TeleportCostsConfig getInstance() {
		return _instance;
	}
	
	/**
	 * 载入配置信息
	 */
	private void loadConfig() {
		FileInputStream fs = null;
		
		try {
			File f = new File(Config.DATAPACK_ROOT, "/data/telepoterCostsList.xml");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			fs = new FileInputStream(f);
			Document document = builder.parse(fs);
			Element root;
			root = document.getDocumentElement();
			Element node = null;
			NodeList list = root.getElementsByTagName("Item");
			
			for (int i = 0; i < list.getLength(); i++) {
				node = (Element)list.item(i);
				String key = node.getAttribute("key");
				String value = node.getAttribute("value");
				costsList.put(key, value);
			}
		} catch(Exception e) {
			_log.error("读取传送扣费配置信息出错", Config.SERVER_ID, e);
		} finally {
			try {
				fs.close();
			} catch(IOException e) {
				_log.error("读取传送扣费配置信息关闭输入流时出错", Config.SERVER_ID, e);
			}
		}
	}
	
	/**
	 * 根据传送路径获取传送价格
	 * @param path
	 * @return
	 */
	public String getPriceByPath(String path) {
		return costsList.get(path);
	}
}
